home *** CD-ROM | disk | FTP | other *** search
- /**amiget/2.0 by alfie**/
- /**libs**/
- signal on break_c
- if ~show("L","rexxsupport.library") then
- if ~addLib("rexxsupport.library",0,-30) then
- call serr("can't find rexxsupport.library")
- if ~show("L","rxsocket.library") then
- if ~addLib("rxsocket.library",0,-30) then
- call serr("can't find rxsocket.library")
- if ~show("L","rmh.library") then
- if ~addLib("rmh.library",0,-30) then
- call serr("can't find rmh.library")
- if ~show("L","rxwiz.library") then
- if ~addLib("rxwiz.library",0,-30) then
- call serr("can't find rxwiz.library"
- if ~show("L","rxasl.library") then
- if ~addLib("rxasl.library",0,-30) then
- call serr("can't find rxasl.library")
- /**args**/
- global.amirc=left(address(),6)=="AMIRC."
- parm.5.value=""
- template="HOST/A,FILE/A,AS/K,PORT/K/N,TIMEOUT/K/N,PUBSCREEN/K,LOGIN/K,PASS/K,HELP/S,PROXY/K,PROXYPORT/K/N,NOCACHE=NC/S,DEBUG=DB/S,INFO/S,RESUME/S"
- if ~RMH_ReadArgs(template) then
- call serr(Fault(IoErr(),"amiget"))
- if parm.8.flag then call serr(template)
- if parm.4.flag & parm.4.value<=0 then
- call serr("timeout must be greater than 0")
- if parm.6.flag~=parm.7.flag then
- call serr("you must supply both login and password")
- if parm.3.flag then do
- port=parm.3.value
- if port<0 | port>65535 then call serr("bad port number")
- end
- else port=80
- if parm.9.flag then do
- if parm.10.flag then proxyport=parm.10.value
- else proxyport=8080
- if proxyport<0 | proxyport>65535 then call serr("bad proxy port number")
- end
- if parm.2.flag then do
- range=""
- saveDir=PathPart(parm.2.value)
- saveFile=FilePart(parm.2.value)
- if saveDir~="" then do
- if word(statef(saveDir),1)~="DIR" then call serr(Fault(IoErr(),"amiget"))
- if saveFile=="" then saveFile=AddPart(saveDir,FilePart(parm.1.value))
- else saveFile=parm.2.value
- end
- else saveFile=parm.2.value
- if parm.14.flag & exists(saveFile) then
- range="Range: "word(statef(saveFile),2)+1"-"
-
- end
- else if parm.14.flag then call serr("for resuming you must specify a file")
- /*is a bsdsocket.library present?*/
- if ~IsLibOn("SOCKET") then do
- call EasyRequest("No Internet stack is running.","amiget",,parm.5.value)
- exit
- end
- /*window*/
- global.sock=-1
- sur.SnapShot=1
- res=OpenSurface("rexx:amiget.wizard","SUR")
- if res~=0 then call serr(GetRxWizString(res))
- if parm.5.flag then win.PubScreen=parm.5.value
- win.Title="Amiget" FilePart(parm.1.value)
- win.Gads=5
- win.ID=1
- res=OpenWindow("SUR","WIN")
- if res~=0 then call serr(GetRxWizString(res))
- global.ws=WindowSignal("SUR","WIN")
- global.hws=0
- /**socket**/
- call SetSocketSignals(or(global.ws,2**12))
- if parm.9.flag then do
- sin.addrPort=proxyport
- sin.addrAddr=Resolve(parm.9.value)
- end
- else do
- sin.addrPort=port
- sin.addrAddr=Resolve(parm.0.value)
- end
- if sin.addrAddr==-1 then call err("Can't find host",1)
- sin.addrFamily="INET"
- global.sock=socket("INET","STREAM")
- if global.sock<0 then call err("No socket")
- /**connection**/
- call info("Connecting...")
- if parm.4.flag then do
- tim=CreateTimer()
- ts=TimerSignal(tim)
- call SetSocketSignals(or(global.ws,ts,2**12))
- call StartTimer(tim,parm.4.value)
- end
- res = connect(global.sock,"SIN")
- err=errno()
- if (res<0) && (err~=35) && (err~=36) then call err("Can't connect")
- /**request**/
- call info("Sending request...")
- if range~="" then httpV = "HTTP/1.1"
- else httpV = "HTTP/1.0"
- if left(parm.1.value,1)~="/" then parm.1.value="/"parm.1.value
- if parm.9.flag then req="GET http://"parm.0.value":"port || parm.1.value httpV
- else req="GET" parm.1.value httpV
- hst="Host:" parm.0.value
- usr="User-Agent: amiget/2.0 (AmigaOS - ARexx - rxsocket.library)"
- if parm.6.flag then auth="Authorization: Basic" encodeB64(parm.6.value":"parm.7.value)
- else auth=""
- if parm.11.flag then cache="Pragma: no-cache"
- else cache=""
- if parm.12.flag then do
- call print "Sending:"
- call print req
- call print usr
- call print range
- call print auth
- call print cache
- call print "-"
- end
- req=req"D0A"x || usr"D0A"x
- if auth~="" then req=req||auth"D0A"x
- if cache~="" then req=req||cache"D0A"x
- if hst~="" then req=req||hst"D0A"x
- if range~="" then req=req||range"D0A"x
- req=req"D0A"x
- if send(global.sock,req)~=length(req) then call err("Send error")
- /**head**/
- call info("Receiving and parsing head...")
- len=recvline(global.sock,"BUF",256)
- if len<0 then call err("Recv error")
- if left(buf,5)~="HTTP/" then call err("Bad answer from server")
- parse var buf "HTTP/"ver code coderest"D0A"x
- go=1
- length=""
- last=""
- mime=""
- hs="HTTP/"ver code coderest"A"x
- do while go
- len=RecvLine(global.sock,"BUF",256)
- if len<0 then call err("Error receiving")
- parse var buf b": "rest"D0A"x
- select
- when b=="Content-Length" then length=rest
- when b=="Last-Modified" then last=rest
- when b=="Content-type" then mime=rest
- when b=="WWW-authenticate" then auth=rest
- otherwise nop
- end
- go=(buf~="D0A"x)
- if go then do
- if parm.12.flag then call print b":" rest
- hs=hs || b":" rest || "A"x
- end
- end
- if parm.4.flag then call FreeTimer(tim)
-
- if parm.13.flag then do
- if parm.5.flag then headwin.PubScreen=parm.5.value
- headwin.Title="Amiget" FilePart(parm.1.value)
- headwin.Gads=5
- headwin.ID=2
- res=OpenWindow("SUR","HEADWIN")
- if res~=0 then call serr(GetRxWizString(res))
- global.hws=WindowSignal("SUR","HEADWIN")
- set.STRINGFIELDTEXT=hs
- res=SetWizAttrs("SUR","HEADWIN","HEADFIELD","SET");drop set.
- if res~=0 then call serr(GetRxWizString(res))
- end
-
- if code~=200 then
- if parm.14.flag & code~=206 then call err("Server error:" code coderest)
- call SetSocketSignals(0)
- if length~="" then l=l || length
- else l=l || "unknown"
- if length~="" then do
- set.GaugeTotal=length
- call SetWizAttrs("SUR","WIN","PROGRESS","SET");drop set.
- end
- call IOCtlSocket(global.sock,"FIONBIO",0)
- call SetSockOpt(global.sock,"SOCKET","EVENTMASK","READ")
- /**savefile**/
- if ~parm.2.flag then do
- call LockWindows("SUR")
- if parm.5.flag then req.PubScreen=parm.5.value
- req.DoSaveMode=1
- req.file=FilePart(parm.1.value)
- if ReqFile("REQ")==0 & req.file~="" then do
- call UnLockWindows("SUR")
- saveFile=AddPart(req.drawer,req.file)
- end
- else exit
- end
- if range=="" then res=Open("OUT",saveFile,"W")
- else res=Open("OUT",saveFile,"A")
- if ~res then call err("Can't open file")
- /**body**/
- dummyL=200
- dummyS=copies(" ",dummyL)
- tl=0
- cps=0
- go=1
- sel.read.0=global.sock
- call time("R")
- stal=0
- do while go
- res = WaitSelect("SEL",0,800000,or(global.ws,global.hws))
- et=time("E")
- if and(sel.signals,global.ws)~=0 then exit
- if and(sel.signals,global.hws)~=0 then call CloseWindow("SUR","HEADWIN")
- ets="ETime:" etime(et)
- if res==0 then do
- stal=stal+1
- if stal>5 then do
- set.GaugeFormat=ets "stalled at" tl "bytes"
- end
- end
- else do
- stal=0
- len=recv(global.sock,"BUF",1024)
- if len<0 then do
- call err("Error Receiving")
- exit
- end
- if len~=0 then do
- if WriteCH("OUT",buf)~=length(buf) then call err("Error writing")
- tl=tl+len
- end
- else go=0
- end
- if stal<=5 then do
- if tl~=0 & et~=0 then cps=(tl/et)%1
- if length~="" then do
- set.GaugeCurrent=tl
- call SetWizAttrs("SUR","WIN","PROGRESS","SET");drop set.
- set.GaugeFormat=ets "Size:" tl || "/"length "(" || (tl*100)%length"%%)"
- end
- else set.GaugeFormat=ets "-" tl "(lenght unknown)"
- set.GaugeFormat=set.GaugeFormat "at" cps "cps"
- end
- set.GaugeFormat=left(set.GaugeFormat||dummyS,dummyL)
- res=SetWizAttrs("SUR","WIN","PROGRESS","SET");drop set.
- end
- et=time("E")
- call CloseSocket(global.sock)
- if length~="" then
- if tl~=length then call err("Aspected" length "- received" tl "bytes")
- set.Label="Exit"
- res=SetWizAttrs("SUR","WIN","ABORT","SET");drop set.
- stop=0
- do while ~stop
- sigrec=Wait(or(global.ws,global.hws))
- if and(sigrec,global.hws)~=0 then call CloseWindow("SUR","HEADWIN")
- else stop=1
- end
- exit
- /**info**/
- info: procedure expose global.
- parse arg msg
- set.GaugeFormat=msg
- res=SetWizAttrs("SUR","WIN","PROGRESS","SET")
- return
-
- /**out**/
- print: procedure expose global.
- parse arg msg
- if global.amirc then
- "ECHO P=" || "1B"x || "bĀ«AmigetĀ» C=6" msg
- else say msg
- return
- /**nowin errors**/
- serr: procedure expose global.
- parse arg msg
- call print msg
- exit
- /**errors**/
- err: procedure expose global.
- parse arg s,res
- if global.sock>=0 then call CloseSocket(global.sock)
- if res==1 then do
- if ~IsLibOn("TTCP") then s=s "("HostErrorString(hosterrorno())")"
- end
- else do
- err=errno()
- if err==4 then do
- s="User break or timeout"
- err=0
- end
- if err~=0 then
- if IsLibOn("TTCP") then s=s "("err")"
- else s=s "("ErrorString(err)")"
- end
- set.Label="Exit"
- res=SetWizAttrs("SUR","WIN","ABORT","SET")
- call info(s)
- stop=0
- do while ~stop
- sigrec=Wait(or(global.ws,global.hws))
- if and(sigrec,global.hws)~=0 then call CloseWindow("SUR","HEADWIN")
- else stop=1
- end
- exit
- /**break_c**/
- BREAK_C:
- exit
- /**B64**/
- encodeB64: procedure
- parse arg s
- s=c2b(s)
- a=""
- do while s~=""
- parse var s c +6 s
- a=a||substr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",c2d(b2c(left(c"0000",6)))+1,1)
- end
- l=length(c)
- if l<6 then a=a||copies("=",(6-l)/2)
- return a
- /**etime**/
- etime: procedure
- parse arg s
- s=s%1
- m=s%60
- s=s//60
- h=m%60
- m=m//60
- return right("00"h,2)":"right("00"m,2)":"right("00"s,2)
- /**that's all**/
-